import { logger } from "../log";
import { WsMsg, WsMsgType } from "../model";
import { sendToClient } from "./sendToClient";

export function handlerWsMsg(param: { id: string; msg: string }) {
  const { id, msg } = param;
  if (!msg) return;
  // 尝试解析
  try {
    const { type, payload } = JSON.parse(msg) as WsMsg<any, any>;

    switch (type) {
      case WsMsgType.PING:
        sendToClient(id, {
          type: WsMsgType.PING,
          payload,
        });
        break;
      default:
        break;
    }
  } catch (e) {
    logger.warn("websocket message parse error", e);
  }
}
